#ifndef IPROTOCOLHANDLER_H
#define IPROTOCOLHANDLER_H

#include <memory>
#include <QByteArray>
#include <QJsonObject>

/**
 * @brief Protocol handler interface for MCP server
 * @details Handles JSON-RPC protocol parsing, serialization, and error response creation
 */
class IProtocolHandler
{
public:
    explicit IProtocolHandler() = default;

    virtual ~IProtocolHandler() = default;

    /**
     * @brief Parse incoming request data
     * @param data - raw byte array data from transport layer
     * @return QJsonObject - parsed JSON-RPC request object
     */
    virtual QJsonObject parseRequest(const QByteArray& data) = 0;

    /**
     * @brief Serialize response to byte array
     * @param response - JSON-RPC response object
     * @return QByteArray - serialized byte array for transport layer
     */
    virtual QByteArray serializeResponse(const QJsonObject& response) = 0;

    /**
     * @brief Create error response object
     * @param code - error code
     * @param message - error message
     * @return QJsonObject - formatted error response
     */
    virtual QJsonObject createErrorResponse(int code, const QJsonValue& message) = 0;
};

/**
 * @brief Message parser interface for JSON-RPC protocol
 * @details Validates and parses JSON-RPC messages according to specification
 */
class IMessageParser
{
public:
    explicit IMessageParser() = default;

    virtual ~IMessageParser() = default;

    /**
     * @brief Validate JSON-RPC message structure
     * @param message - JSON message to validate
     * @return bool - true if message is valid JSON-RPC
     */
    virtual bool validateMessage(const QJsonObject& message) = 0;

    /**
     * @brief Parse method name from request
     * @param request - JSON-RPC request object
     * @return QString - method name
     */
    virtual QString parseMethodName(const QJsonObject& request) = 0;

    /**
     * @brief Parse parameters from request
     * @param request - JSON-RPC request object
     * @return QJsonObject - parameters value
     */
    virtual QJsonObject parseParameters(const QJsonObject& request) = 0;

    /**
     * @brief Check if message is a notification
     * @param message - JSON-RPC message
     * @return bool - true if message is notification (no id field)
     */
    virtual bool isNotification(const QJsonObject& message) = 0;

};

/**
 * @brief Message serializer interface for JSON-RPC protocol
 * @details Serializes JSON-RPC responses and notifications
 */
class IMessageSerializer
{
public:
    explicit IMessageSerializer() = default;

    virtual ~IMessageSerializer() = default;

    /**
     * @brief Serialize success response
     * @param id - request ID, the ID must exist!
     * @param result - result value
     * @return QJsonObject - serialized success response
     */
    virtual QJsonObject serializeSuccessResponse(const QVariant& id, const QJsonObject& result) = 0;

    /**
     * @brief Serialize error response
     * @param id - request ID, the ID must exist!
     * @param errorCode - error code
     * @param errorData - optional error data
     * @return QJsonObject - serialized error response
     */
    virtual QJsonObject serializeErrorResponse(const QVariant& id, int errorCode, const QJsonValue& errorData = QJsonValue()) = 0;

    /**
     * @brief Serialize notification
     * @param method - method name
     * @param params - parameters
     * @return QJsonObject - serialized notification
     */
    virtual QJsonObject serializeNotification(const QString& method, const QJsonObject& params) = 0;

    /**
     * @brief Convert JSON object to byte array
     * @param jsonObject - JSON object to serialize
     * @return QByteArray - serialized byte array
     */
    virtual QByteArray toByteArray(const QJsonObject& jsonObject) = 0;
};

#endif // IPROTOCOLHANDLER_H
